<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<%@ page import="java.util.TreeMap"%> 
<%@ page import="java.util.ArrayList"%>  
<%@ page import="cleanstarxchange.server.*"%> 
<%@ page import="com.socialjava.*"%>


	<script language="javascript">
		<!--
		function toMain()
		{
			
   			var frm = document.frm;
	   		frm.action = "index.jsp";
   			frm.submit();
		}
		//-->
	</script>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>StarXChange</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
<!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColElsLtHdr #sidebar1 { padding-top: 30px; }
.twoColElsLtHdr #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]--></head>
<body class="twoColElsLtHdr">
 
 

<%
		//getting the sessionkey from facebook
		String sessionKey = request.getParameter("fb_sig_session_key");
		
		//initialize uid for facebook userID
		String uid = new String();
		
		//initialize info for the information of the user
		String info = new String();
		
		//initialize the infospliter for seperating 
		//lastname and first name of the user from 
		//the information of the user 
		String[] infoSplit;
		
		//initialize firstname for the firstname of the user
		String firstName= new String();
		
		//initialize lastname for the lastname of the user
		String lastName = new String();
		
		//apikey, and secretKey for the facebook app
		String apiKey = "da4966aa1343e2d06cf00512c0fdc94e";
		String secretKey = "c286ceb3cd2caf11838c1f7ef433ccc5";
		
		//if the user is not logged in to facebook redirect the page to log in page
		if (sessionKey == null) 
			response.sendRedirect("http://www.facebook.com/login.php?api_key=" + apiKey + "&v=1.0"); 
		//if the user is logged in continue on our application process
		else 
		{	 
			//initializing tinyfacebookclient for getting information from facebook easier
			TinyFBClient fb = new TinyFBClient(apiKey, secretKey, sessionKey);
			//fb.setFormat("XML");
			
			//set the sessionkey for connection to facebook
			fb.setSession(sessionKey);
			
			//getting the userID from the facebook
			request.setAttribute("tinyFBClient", fb);
			TreeMap<String, String> tm = new TreeMap<String, String>();
			tm.put("uid", "");
			uid = fb.call("Users.getLoggedInUser",tm);
			
			//getting the first and last name of the user from the facebook
			TreeMap<String, String> parameters = new TreeMap<String, String>();
			String fields = "first_name,last_name";
	        parameters.put("uids", uid);
	        parameters.put("fields", fields);
	        info = fb.call("Users.getInfo",parameters);
	        
	        //by using the splitter, getting the first and the last name seperately
	        String dq= ""+ (char)34+ "";
	        infoSplit = info.split(dq);
	        firstName =infoSplit[3];
	        lastName = infoSplit[7];	
		}	
		
		//initializing datastore object for saving objects to datastore
		DataStore ds = new DataStore();
		ds.updateStocksAndPort();
		
		//storing the current user to datastore with one's name and initial balance  
		User user = ds.getUser( new Long(uid) ); 
		if ( user == null )
			ds.insertUser( new User( Integer.parseInt(uid), firstName, lastName, true, 10000.0 ) );
		ds.close();
%>
<!-- container wraps the whole contents of the webpage -->
<div id="container">
  <!-- header contains the logo of our application which links to index.jsp page -->
  <div id="header"><a href="index.jsp?uid=<%=uid %>"><img src="media/logo.jpg"/></a><!-- end #header --></div>
  <!-- login contains the greeting msg to the user and a button to play the game -->
  <div id="login">
    	<div>		
			<!-- these two scripts are needed for the connection to the facebook
			     for getting the name and profile picture -->
			<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
			<script type="text/javascript">
   			 	FB.init("da4966aa1343e2d06cf00512c0fdc94e", "xd_receiver.htm");
			</script>
		</div>
		
		<!-- displaying the facebook profile picture of the user -->
		<div align="center">
			<fb:profile-pic uid="<%=uid %>" size="small" linked="true"></fb:profile-pic>
		</div>
    	
    	<!--  greeting comment and a button -->
    	<form name="frm" method="post">
      		<div align="center"><p>Welcome <%=firstName%>!</p></div>
      		<div align="center"><p>Are you ready to trade?</p></div>
			<div align="center"><a href="index.jsp?uid=<%=uid %>"> <img src="media/FB.jpg"/></a></div>
  		</form>
  	
  	</div>
    <!-- end #mainContent --></div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
   <br class="clearfloat" />
   <div id="footer">
    <p>Ranks will be here</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>